%move the ship
iteration=0;
n=2;
p=0.25;                          %condition: p<1/n

start;                          %first step

for i=1:20

a=rand(1);
if a>1-n*p                      %same direction (n possibilities)
    r=n*rand(1);                %choose a number between '0' and 'n'
    r=ceil(r);                  %r becomes integer: 1, 2, ..., n
    vari=last+r;
    vari=mod(vari,n+1);
    if isempty(vector); moveahead;
    else
        if vector(end)==vari; moveback;
        else moveahead;
        end
    end
else                            %go back (1 possibility)
    if isempty(vector)
        vari=last;
        moveahead;
    else
        if vector(end)==last, moveback;              
        else vari=last;
        moveahead;
        end
    end
end
vector
end

